Skip to content

Conversation

@weswigham
Copy link
Member

keyof never -> string | number | symbol (changed from never)
keyof unknown -> never (unchanged, but no longer derived from unknown's empty apparent type)

The reasoning for the first is pretty simple - we know keyof is contravariant, so the more specialized the input type is, the broader the output type is. For each key we add on the input type, we specify a smaller and smaller set of inputs, while the output set of keys grows. Once the input is the empty set of types (never), our result should encompass every possible key.

Fixes #30657

@weswigham
Copy link
Member Author

@typescript-bot test this & @typescript-bot run dt - I honestly have no idea what this will affect in the wild. Historically people haven't really relied on incorrect boundary relations in TS, so we shall see.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 4, 2019

Heya @weswigham, I've started to run the extended test suite on this PR at d147a0d. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 4, 2019

Heya @weswigham, I've started to run the Definitely Typed test suite on this PR at d147a0d. You can monitor the build here. It should now contribute to this PR's status checks.

@jack-williams
Copy link
Collaborator

This is great change IMO. One semi-related question. Is there a reason for the following, mainly regarding number:

type A = keyof Object; //  "constructor" | "toString" | "toLocaleString" | "valueOf" | "hasOwnProperty" | "isPrototypeOf" | "propertyIsEnumerable"
type B = keyof number; //  "toString" | "toFixed" | "toExponential" | "toPrecision" | "valueOf" | "toLocaleString"
type C = keyof object; //  never;

@weswigham
Copy link
Member Author

@RyanCavanaugh @ahejlsberg no DT or RWC changes 😸

@weswigham weswigham merged commit d405662 into microsoft:master Apr 16, 2019
@weswigham weswigham deleted the keyof-never branch April 16, 2019 01:01
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type simultaneously does and does not have property

3 participants